/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.main.header;

import javax.swing.JLabel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfonia.connect5.basics.Observer;
import org.jfonia.constants.SymbolConstants;
import org.jfonia.constants.ViewConstants;
import org.jfonia.language.LabelConstants;
import org.jfonia.language.LanguageResource;
import org.jfonia.notation.Notation;

/**
 *
 * @author Rik Bauwens
 */
public class NotePanel extends NotationPanel
{
        public NotePanel()
        {
                super(LanguageResource.getInstance().getLabel(LabelConstants.NOTES));
        }

        @Override
        protected void init()
        {
                JLabel noteHeadLabel = new JLabel(LanguageResource.getInstance().
                        getLabel(LabelConstants.NOTEHEAD));
                addLabel(noteHeadLabel);
                GlyphLabel noteHead1 = new GlyphLabel(this, SymbolConstants.NOTEHEAD_1,
                                                      NotationType.NOTEHEAD, ViewConstants.NOTEHEAD1);
                addOption(noteHead1);
                GlyphLabel noteHead1_2 = new GlyphLabel(this, SymbolConstants.NOTEHEAD_1_2,
                                                        NotationType.NOTEHEAD, ViewConstants.NOTEHEAD1_2);
                addOption(noteHead1_2);
                GlyphLabel noteHead1_4 = new GlyphLabel(this, SymbolConstants.NOTEHEAD_1_4,
                                                        NotationType.NOTEHEAD, ViewConstants.NOTEHEAD1_4);
                addOption(noteHead1_4);
                addSpacing();

                JLabel accidentalLabel = new JLabel(LanguageResource.getInstance().
                        getLabel(LabelConstants.ACCIDENTAL));
                addLabel(accidentalLabel);
                GlyphLabel accDoubleSharp = new GlyphLabel(this, SymbolConstants.ACC_DOUBLE_SHARP,
                                                           NotationType.ACCIDENTAL,
                                                           ViewConstants.ACC_DOUBLE_SHARP);
                addOption(accDoubleSharp);
                GlyphLabel accSharp = new GlyphLabel(this, SymbolConstants.ACC_SHARP,
                                                           NotationType.ACCIDENTAL,
                                                           ViewConstants.ACC_SHARP);
                addOption(accSharp);
                GlyphLabel accNatural = new GlyphLabel(this, SymbolConstants.ACC_NATURAL,
                                                           NotationType.ACCIDENTAL,
                                                           ViewConstants.ACC_NATURAL);
                addOption(accNatural);
                GlyphLabel accFlat = new GlyphLabel(this, SymbolConstants.ACC_FLAT,
                                                           NotationType.ACCIDENTAL,
                                                           ViewConstants.ACC_FLAT);
                addOption(accFlat);
                GlyphLabel accDoubleFlat = new GlyphLabel(this, SymbolConstants.ACC_DOUBLE_FLAT,
                                                           NotationType.ACCIDENTAL,
                                                           ViewConstants.ACC_DOUBLE_FLAT);
                addOption(accDoubleFlat);
                addSpacing();

                JLabel flagsLabel = new JLabel(LanguageResource.getInstance().
                        getLabel(LabelConstants.FLAGS));
                addLabel(flagsLabel);
                final SpinnerModel flagsModel =
                        new SpinnerNumberModel(Notation.getInstance().getFlags(),
                                               ViewConstants.MIN_FLAGS,
                                               ViewConstants.MAX_FLAGS,
                                               ViewConstants.STEP_FLAGS);
                final CustomSpinner flags = new CustomSpinner(flagsModel);
                //final HeaderTextField flags = new HeaderTextField(Integer.toString(Notation.getInstance().getFlags()),
                //                              ViewConstants.DEFAULT_TEXTBOX_SIZE);
                Notation.getInstance().getFlagsNode().addObserver(new Observer(){
                        public void onNotify(Object source)
                        {
                                if(((Integer) flagsModel.getValue()).intValue() != Notation.getInstance().getFlags())
                                        flagsModel.setValue(Notation.getInstance().getFlags());
                        }
                });
                Notation.getInstance().getNoteHeadNode().addObserver(new Observer(){
                        public void onNotify(Object source)
                        {
                                flags.setEnabled(Notation.getInstance().getNoteHead() == ViewConstants.NOTEHEAD1_4 ? true : false);
                        }
                });
                flagsModel.addChangeListener(new ChangeListener(){
                        public void stateChanged(ChangeEvent e)
                        {
                                if(Notation.getInstance().getFlags() != ((Integer) flagsModel.getValue()).intValue())
                                        Notation.getInstance().setFlags((Integer) flagsModel.getValue());
                        }
                });
                flags.setEnabled(Notation.getInstance().getNoteHead() == ViewConstants.NOTEHEAD1_4);
                flags.setMaximumSize(flags.getPreferredSize());
                addOption(flags);
                addSpacing();

                JLabel dotsLabel = new JLabel(LanguageResource.getInstance().
                        getLabel(LabelConstants.DOTS));
                addLabel(dotsLabel);
                final SpinnerModel dotsModel =
                        new SpinnerNumberModel(Notation.getInstance().getNoteDots(),
                                               ViewConstants.MIN_DOTS,
                                               ViewConstants.MAX_DOTS,
                                               ViewConstants.STEP_DOTS);
                final CustomSpinner dots = new CustomSpinner(dotsModel);
                Notation.getInstance().getNoteDotsNode().addObserver(new Observer(){
                        public void onNotify(Object source)
                        {
                                if(((Integer) dotsModel.getValue()).intValue() != Notation.getInstance().getNoteDots())
                                        dotsModel.setValue(Notation.getInstance().getNoteDots());
                        }
                });
                dotsModel.addChangeListener(new ChangeListener(){
                        public void stateChanged(ChangeEvent e)
                        {
                                if(Notation.getInstance().getNoteDots() != ((Integer) dotsModel.getValue()).intValue())
                                        Notation.getInstance().setNoteDots((Integer) dotsModel.getValue());
                        }
                });
                dots.setMaximumSize(dots.getPreferredSize());
                addOption(dots);
        }
}
